﻿
using Autofac.Extras.DynamicProxy;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Truron.Purchase.Interface;
using Truron.Purchase.Model;
using Truron.SysCenter.Model;
using Truron.Purchase.Service.DbContext;
using Truron.Utility;
using Truron.Utility.RedisHelper;

namespace Truron.Purchase.Service
{
	
	
	    
    [Intercept(typeof(CustomAutofacAop))]
    public class Purchase_OtherDataService
    {
        public Purchase_OtherDataService()
        {
            Init(true);
        }
        private List<Purchase_OtherData> Init(bool isInit = false)
        {
            try
            {
                if (isInit)//初始化
                    RedisBase.Hash_Remove(StaticConstraint.PurchaseCategoryCacheKey);
                var List = RedisBase.Hash_GetAll<Purchase_OtherData>(StaticConstraint.Purchase_OtherDataCacheKey);
                if (List != null && List.Count > 0)
                    return List;

                List = DapperDbContext.Query<Purchase_OtherData>("select * from [Purchase_OtherData] where Status != @Status", new { Status = Status.delete }).ToList();
                if (List.Count > 0)
                {
                    foreach (var item in List)
                    {
                        RedisBase.Hash_Set(StaticConstraint.Purchase_OtherDataCacheKey, item.Id, item);
                    }
                }
                return List;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"初始化单据数据失败,isInit:{isInit},详细:{ex.ToString()}");
            }
            return new List<Purchase_OtherData>();
        }
        public (int count, List<Purchase_OtherData> list) GetList(string searchKey, int pageSize, int pageIndex,string searchJson)
        {
            int pageNum = (pageIndex - 1) * pageSize;
            var List = Init().ToList();
            var ParamList = List.Skip(pageNum).Take(pageSize).ToList();
            return (List.Count, ParamList);
        }
        public Purchase_OtherData GetDetail(int Id)
        {
            return Init().Where(x => x.Id == Id).FirstOrDefault();
        }
        public int Del(List<int> Ids,UserCache user)
        {
            try
            {
                using (var conn = DapperHelper.Instance.OpenCurrentDbConnection())
                {
                    string sql = "UPDATE [dbo].[Purchase_OtherData] set [Status] = @Status,[LastUpdateUser] = @LastUpdateUser,[LastUpdateTime] = @LastUpdateTime where Id in @Id";
                    int delcount = conn.Execute(sql, new { Status = Status.delete, LastUpdateUser = user.UserId, LastUpdateTime = DateTime.Now, Id = Ids });
                    if(delcount>0)
                    {
                        foreach (var id in Ids)
                        {
                            RedisBase.Hash_Remove(StaticConstraint.Purchase_OtherDataCacheKey, id.ToString());
                        }
                    }
                    return delcount;
                }
                    
            }
            catch (Exception ex)
            {

                return -1;
            }
           
        }
        public List<Purchase_OtherData> GetListAll()
        {
            return Init().ToList();
        }
        public List<Purchase_OtherData> GetListByIds(List<int> ids)
        {
            return Init().Where(x=>ids.Contains(x.Id)).ToList();
        }
    }
}